package com.ylsk.check.service.impl;

import java.util.Date;
import java.util.List;

import com.ylsk.check.domain.*;
import com.ylsk.check.domain.dto.RectificationReviewDTO;
import com.ylsk.check.domain.emum.CheckTableEnum;
import com.ylsk.check.domain.emum.ConstructionStatusEnum;
import com.ylsk.check.domain.so.RectificationReviewSO;
import com.ylsk.check.domain.vo.RectificationReviewVO;
import com.ylsk.check.mapper.CheckProblemMapper;
import com.ylsk.check.mapper.CheckRecordMapper;
import com.ylsk.check.mapper.WithdrawalReportMapper;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.system.domain.FileSimple;
import com.ylsk.system.helper.FileHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.RectificationReviewMapper;
import com.ylsk.check.service.IRectificationReviewService;

/**
 * 整改复查Service业务层处理
 *
 * @author ysy
 * @date 2023-12-26
 */
@Service
public class RectificationReviewServiceImpl implements IRectificationReviewService
{
    @Autowired
    private RectificationReviewMapper rectificationReviewMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private FileHelper fileHelper;

    @Autowired
    private CheckRecordMapper checkRecordMapper;

    @Autowired
    private CheckProblemMapper checkProblemMapper;

    @Autowired
    private ReportInfoHelper helper;

    @Autowired
    private WithdrawalReportMapper withdrawalReportMapper;
    /**
     * 查询整改复查
     *
     * @param rectificationReviewId 整改复查主键
     * @return 整改复查
     */
    @Override
    public RectificationReviewVO selectRectificationReviewByRectificationReviewId(Long rectificationReviewId)
    {
        RectificationReviewVO rectificationReviewVO = rectificationReviewMapper.selectRectificationReviewByRectificationReviewId(rectificationReviewId);
        Long reviewFileId = rectificationReviewVO.getReviewFileId();
        List<FileSimple> fileSimpleList = fileHelper.getFileByBusinessId(reviewFileId);
        rectificationReviewVO.setReviewFile(fileSimpleList);
        return rectificationReviewVO;
    }

    /**
     * 查询整改复查列表
     *
     * @param rectificationReviewSO 整改复查
     * @return 整改复查
     */
    @Override
    public List<RectificationReviewVO> selectRectificationReviewList(RectificationReviewSO rectificationReviewSO)
    {
        return rectificationReviewMapper.selectRectificationReviewList(rectificationReviewSO);
    }

    /**
     * 新增整改复查
     *
     * @param rectificationReviewDTO 整改复查
     * @return 结果
     */
    @Override
    public int insertRectificationReview(RectificationReviewDTO rectificationReviewDTO)
    {
        Long checkProblemId = rectificationReviewDTO.getCheckProblemId();
        if(null == checkProblemId){
            throw new ServiceException("施工检查问题id不能为空!");
        }
        RectificationReview rectificationReview = new RectificationReview();
        BeanUtils.copyProperties(rectificationReviewDTO,rectificationReview);

        //  附件内容
        String reviewFile = rectificationReviewDTO.getReviewFile();
        final Long reviewFileId = snowIdConfig.getSnowId();
        if(StringUtils.isNotEmpty(reviewFile)){
            fileHelper.saveBusinessFileData(reviewFile,reviewFileId);
        }
        //  复查人设置成  当前登录人
        rectificationReview.setReviewBy(SecurityUtils.getUserId());
        //  所属管辖大队
        rectificationReview.setDeptId(SecurityUtils.getDeptId());
        rectificationReview.setReviewFileId(reviewFileId);
        rectificationReview.setRectificationReviewId(snowIdConfig.getSnowId());

        //todo  复查的时候 是否需要归档  和 更改施工状态
        //CheckProblem checkProblem = checkProblemMapper.selectById(checkProblemId);
        //CheckRecord checkRecord = checkRecordMapper.selectById(checkProblem.getCheckRecordId());
        //  整改复查，检查信息  为今日撤场
//        if(checkRecord != null && StringUtils.isNotEmpty(checkRecord.getTbName()) && checkRecord.getTbName().equals(CheckTableEnum.INS_WITHDRAWAL_REPORT.getCode()) ){
//            WithdrawalReport withdrawalReport = withdrawalReportMapper.selectById(checkRecord.getReportBusinessId());
//            if(null == withdrawalReport){
//                throw new ServiceException("参数异常,撤场上报id为空.");
//            }
//            if(withdrawalReport.getEndProject() != null && withdrawalReport.getEndProject() == 1){
//                ReportInfo reportInfo = helper.getReportInfo(checkRecord.getReportInfoId());
//                //  工程状态为已归档
//                reportInfo.setIsArchived(1);
//                //  施工状态为结束
//                helper.updateReportConstructionStatus(reportInfo,ConstructionStatusEnum.END);
//            }
//        }
        Date reviewTime = rectificationReviewDTO.getReviewTime();
        rectificationReview.setReviewTime(reviewTime == null ? new Date() : reviewTime);
        return rectificationReviewMapper.insert(rectificationReview);
    }

    /**
     * 修改整改复查
     *
     * @param rectificationReviewDTO 整改复查
     * @return 结果
     */
    @Override
    public int updateRectificationReview(RectificationReviewDTO rectificationReviewDTO)
    {
        final Long rectificationReviewId = rectificationReviewDTO.getRectificationReviewId();
        if(null == rectificationReviewId){
            throw new ServiceException("rectificationReviewId 不能为空!");
        }
        RectificationReview rectificationReview = new RectificationReview();
        BeanUtils.copyProperties(rectificationReviewDTO,rectificationReview);
        return rectificationReviewMapper.updateById(rectificationReview);
    }

    /**
     * 批量删除整改复查
     *
     * @param rectificationReviewIds 需要删除的整改复查主键
     * @return 结果
     */
    @Override
    public int deleteRectificationReviewByRectificationReviewIds(Long[] rectificationReviewIds)
    {
        return rectificationReviewMapper.deleteRectificationReviewByRectificationReviewIds(rectificationReviewIds);
    }

    /**
     * 删除整改复查信息
     *
     * @param rectificationReviewId 整改复查主键
     * @return 结果
     */
    @Override
    public int deleteRectificationReviewByRectificationReviewId(Long rectificationReviewId)
    {
        return rectificationReviewMapper.deleteRectificationReviewByRectificationReviewId(rectificationReviewId);
    }
}
